/**
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

{%- if item.method_return_type == "String" %}
function compareStringTrivial(actual : String, expected : String) : boolean {
  if (actual == expected) return true;
  return false;
}
{%- endif %}

{%- if item.method_return_type == "StringBuilder" %}
function compareStringBuilderWithString(actual : StringBuilder, expected : String) : boolean {
    let tmpString = actual.toString();
    return expected.equals(tmpString);
  }
{%- endif %}

{%- if item.result_nature == "primitive" or item.method_return_type == "UTF_16_CodePoint" or item.method_return_type == "char" or item.method_return_type == "byte" or item.method_return_type == "short" or item.method_return_type == "int" or item.method_return_type == "long" or item.method_return_type == "boolean" or item.method_return_type == "number" or item.method_return_type == "bigint" %}
{%- if item.method_return_type != "void" %}
function comparePrimitiveValue(actual : {{.item.method_return_type}}, expected : {{.item.method_return_type}}) : boolean {
{%- else %}
function comparePrimitiveValue(actual : {{.item.result_type}}, expected : {{.item.result_type}}) : boolean {
{%- endif %}
  if (actual == expected) return true;
  return false;
}
{%- endif %}

{%- if item.method_return_type == "float" or item.method_return_type == "double" or item.method_return_type == "Float" or item.method_return_type == "Double" %}
const DBL_EPSILON : double = 1.0e-6;
function compareFloatPointValue(left : double, right : double) : boolean {
    if (left == right) {
      return true;
    }
    let diff : double;
    if (left > right) {
       diff = left - right;
    } else {
       diff = right -left;
    }
    if (diff < DBL_EPSILON) {
       return true;
    } else {
       return false;
    }
}
{% endif %}

{%- if item.method_return_type == "Char" or item.method_return_type == "Boolean" or item.method_return_type == "Byte" or item.method_return_type == "Short" or item.method_return_type == "Int" or item.method_return_type == "Long" or item.method_return_type == "Float" or item.method_return_type == "Double" %}
function compareWrapperObjectValue(actual : {{.item.method_return_type}}, expected : {{.item.expected_data_item_type}}) : boolean {
    let actual2 : {{.item.expected_data_item_type}};
    actual2 = actual.unboxed();
  {%- if item.method_return_type == "Float" or item.method_return_type == "Double" %}
    return compareFloatPointValue(actual2, expected);
  {% else %}
    if (actual2 == expected) return true;
    return false;
  {%- endif %}
}
{%- endif %}

{%- if item.method_name == "compareTo" %}
function compareCompareToResult(actual : {{.item.method_return_type}}, expected : {{.item.method_return_type}}) : boolean {
    if (expected > 0 && actual > 0) {
      return true
    } else  if (expected == 0 && actual == 0) {
      return true
    } else  if (expected <0 && actual <0) {
      return true;
    } else {
      return false;
    }
 }
 {% endif %}

{%- if item.result_nature == "array" %}
{%- if item.method_return_type != "void" %}
function comparePrimitiveArray(actual :  {{.item.method_return_type}}, expected : {{.item.expected_test_data_item_type}}) : boolean {
{% else %}
function comparePrimitiveArray(actual :  {{.item.param_types[item.result_storage]}}, expected : {{.item.expected_test_data_item_type}}) : boolean {
{%- endif %}
   if (actual.length != expected.length) return false;
   for (let i = 0; i < actual.length; i++) {
     if (actual[i] != expected[i]) return false;
   }

   return true;
 }
{%- endif %}

{%- if item.result_nature == "RegExpMatchArray" %}
function compareRegExpMatchArray(actual : {{.item.method_return_type}}, expected : {{.item.expected_test_data_item_type}}) : boolean {
  if (actual == null && expected == null) {
    return true;
  }
  if (actual == null || expected == null) {
    return false;
  }
  for (let i = 0; i < actual.length; i++) {
     if (actual.result[i] != expected[i]) return false;
   }
  return true;
}
{%- endif %}

{% if item.verify_region is defined %}
function compareRegion(start_region : {{.item.method_return_type}}, end_region : {{.item.method_return_type}}, actual : {{.item.method_return_type}}) : boolean {
  if ( actual {{.item.start_region_check_operation}} start_region && actual {{.item.end_region_check_operation}} end_region ) return true;
  return false;
}
{% endif %}


{% if item.verify_test_result_function == "compareToNAN" %}
function compareToNAN(actual: {{.item.object_type}}, ignored: {{.item.object_type}}): boolean {
    return {{.item.object_type}}.isNaN(actual);
}
{% endif %}

{%- if item.method_return_type == "Date"  %}
  function compareDateValueWithString(actual : Date, expected : String) : boolean {
    let expectedTmp : String = expected + " " +  {{.item.expected_time_zone}};
    let actualString : String = actual.toString();

    if (actualString == expectedTmp) return true
    console.println("Actual: " + actualString + ";")
    console.println("Expected: " + expectedTmp + ";")
    return false;
  }

{%- endif %}
